#include "player.h"
#include "ui_player.h"
#include<qbitmap.h>
#include<qpainter.h>
#include<qdebug.h>
Player::Player(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Player)
{
    ui->setupUi(this);
    //创建slider
    sld=new mySlider(this);

    playlist=new QMediaPlaylist;
    playlist->addMedia(QUrl("qrc:/media/sound/player_retro.wav"));
    playlist->addMedia(QUrl("qrc:/media/sound/bradbury.wav"));
    playlist->addMedia(QUrl("qrc:/media/sound/miami.wav"));
    playlist->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop);
    playlist->setCurrentIndex(0);
    plr=new QMediaPlayer;
    plr->setPlaylist(playlist);//一定要在实例化后使用指针，否则会崩溃

    //窗口ui
    this->setWindowFlags(Qt::FramelessWindowHint);
    this->setStyleSheet("background-color: rgb(220, 220, 220);");

    //设置圆角窗口
    QBitmap bmp(this->size());
    bmp.fill();
    QPainter pt(&bmp);
    pt.setPen(Qt::NoPen);
    pt.setBrush(Qt::black);
    pt.drawRoundedRect(bmp.rect(),15,15);
    setMask(bmp);

    //设置list
    myList=new QListWidget(this);
    myList->setGeometry(800,150,350,400);
    myList->setIconSize(QSize(500,50));
    myList->setStyleSheet("border-radius: 5px;");
    items[0]=new QListWidgetItem("Retro",myList,0);
    items[1]=new QListWidgetItem("Bradbury Building Hotel",myList,1);
    items[2]=new QListWidgetItem("Miami Rainday",myList,2);
    connect(myList,SIGNAL(currentRowChanged(int)),this,SLOT(listSlot()));

    //play按钮
    playbtn=new QPushButton(this);
    playbtn->setGeometry(900,570,150,40);
    playbtn->setText("play");
    playbtn->setFont(QFont("Bahnschrift"));
    playbtn->setStyleSheet("border-radius: 5px;");
    playbtn->show();
    connect(playbtn,SIGNAL(clicked()),this,SLOT(play()));


    //设置封面显示界面
    cover=new QWidget(this);
    cover->setGeometry(50,50,700,700);
    cover->setStyleSheet("border-radius:10px;border-image: url(:/pic/pic/retro3.png);");
}

Player::~Player()
{
    delete ui;
}

void Player::play(){
    if(plr->state()==0){
        sld->setMaximum(plr->duration());
        plr->play();
        connect(plr,SIGNAL(positionChanged(qint64)),this,SLOT(sliderChange()));
        playbtn->setText("pause");
        playbtn->setFont(QFont("Bahnschrift"));
        return;//这里必须要return，否则3个if都会执行
    }
    if(plr->state()==1){
        plr->pause();
        playbtn->setText("play");
        playbtn->setFont(QFont("Bahnschrift"));
        return;
    }
    if(plr->state()==2){
        plr->play();
        playbtn->setText("pause");
        playbtn->setFont(QFont("Bahnschrift"));
        return;
    }
}

void Player::sliderChange(){

    sld->setValue(plr->position());

}

void Player::listSlot(){
    switch(myList->currentRow()){
    case 0:{
        playlist->setCurrentIndex(0);
        cover->setStyleSheet("border-radius:10px;border-image: url(:/pic/pic/retro3.png);");
        break;
    }
    case 1:{
        playlist->setCurrentIndex(1);
        cover->setStyleSheet("border-radius:10px;border-image: url(:/pic/pic/hotel.png);");
        break;
    }
    case 2:{
        playlist->setCurrentIndex(2);
        cover->setStyleSheet("border-radius:10px;border-image: url(:/pic/pic/miamirain.png);");
        break;
    }
    default:;

    }
}

void Player::on_pushButton_clicked()
{
    plr->stop();
    this->close();
}

void Player::progressChange(){

    plr->setPosition(sld->value());
}
